package cn.junechiu.mytodo

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import butterknife.BindView
import butterknife.ButterKnife
import io.realm.Realm
import org.jetbrains.anko.sdk25.coroutines.onClick

class MainActivity : AppCompatActivity() {

    @BindView(R.id.toolbar)
    lateinit var toolbar: Toolbar

    @BindView(R.id.fab)
    lateinit var fab: FloatingActionButton

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ButterKnife.bind(this)
        setSupportActionBar(toolbar)
        initView()
        initData()
    }

    fun initView() {
        toolbar.setTitleTextColor(resources.getColor(R.color.cffffff))
        fab.onClick {
            hideFab()
            val todoEditFragment = TodoEditFragment()
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.content_main, todoEditFragment, todoEditFragment.javaClass.getSimpleName())
                    .addToBackStack(todoEditFragment.javaClass.getSimpleName())
                    .commit()
//            Snackbar.make(fab, "Replace with your own action", Snackbar.LENGTH_LONG)
//                    .setAction("Action", null).show()
        }
    }

    fun initData() {
        // 待办事项 List
        val fragment = TodosFragment.newInstance()
        supportFragmentManager.beginTransaction()
                .replace(R.id.content_main, fragment, fragment::class.java.getSimpleName())
                .commit()

        supportFragmentManager.addOnBackStackChangedListener {
            val backStackCount = supportFragmentManager.backStackEntryCount
            if (backStackCount == 0) {
                showFab()
            }
        }
    }

    override fun onResume() {
        super.onResume()
//        val todos = Realm.getDefaultInstance().where(Todo::class.java).findAll()
//        Log.i("TodoApp", "onResume: ${todos}")
    }

    fun hideFab() {
        fab?.visibility = View.GONE
    }

    fun showFab() {
        fab?.visibility = View.VISIBLE
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        val id = item.itemId
        if (id == R.id.action_settings) {
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}
